package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.HoaDonDao;
import com.tt.fashionWebSite.dao.HoaDonFullDao;
import com.tt.fashionWebSite.dao.PhanQuyenDao;
import com.tt.fashionWebSite.dao.ThongTinThanhVienDao;
import com.tt.fashionWebSite.domain.HoaDonFull;
import com.tt.fashionWebSite.domain.PhanQuyen;
import com.tt.fashionWebSite.domain.ThongTinThanhVien;
import com.tt.fashionWebSite.dto.HoaDonDto;
import com.tt.fashionWebSite.dto.HoaDonFullDto;
import com.tt.fashionWebSite.dto.PhanQuyenDto;
import com.tt.fashionWebSite.dto.ThongTinThanhVienDto;
import com.tt.fashionWebSite.service.EditAccountService;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.util.AutoMailTask;
import com.tt.fashionWebSite.util.CryptographyDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by isoft-work 5 on 11/16/14.
 */
@Service("editAccountService")
public class editAccountServiceImp extends GenericService implements EditAccountService {
   @Autowired
   @Qualifier("thongTinThanhVien")
   private ThongTinThanhVienDao thongTinThanhVienDao;

   @Autowired
   @Qualifier("phanQuyen")
   private PhanQuyenDao phanQuyenDao;

   @Autowired
   @Qualifier("hoaDon")
   private HoaDonDao hoaDonDao;

   @Autowired
   @Qualifier("hoaDonFull")
   private HoaDonFullDao hoaDonFullDao;

   @Override
   public ThongTinThanhVienDto getInfoUserByEmail(ThongTinThanhVienDto user) {
      ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(user.getMaTV());
      ThongTinThanhVienDto thongTinThanhVienDto = mapper.map(thongTinThanhVien, ThongTinThanhVienDto.class, THONG_TIN_THANH_VIEN_MAP_ID);
      return thongTinThanhVienDto;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public  ThongTinThanhVienDto updateInfoUser(ThongTinThanhVienDto user){
      try {
         ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(user.getMaTV());
         thongTinThanhVien.setDiaChi(user.getDiaChi());
         thongTinThanhVien.setTen(user.getTen());
         thongTinThanhVien.setHo(user.getHo());
         thongTinThanhVien.setSoDT(user.getSoDT());
         thongTinThanhVien.setGioiTinh(user.getGioiTinh());
         thongTinThanhVien.setNgaySinh(user.getNgaySinh());
         thongTinThanhVienDao.update(thongTinThanhVien);
         return user;
      }
      catch (Exception e) {
         return user = null;
      }
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public PhanQuyenDto changePass(PhanQuyenDto user) {
      try {
         PhanQuyen phanQuyen = phanQuyenDao.findById(user.getThongTinThanhVien().getMaTV());
         phanQuyen.setMatKhau(CryptographyDataUtils.encryptWithAES(user.getMatKhau()));
         phanQuyenDao.update(phanQuyen);

         // send mail
         AutoMailTask autoMail = new AutoMailTask();
         autoMail.sendMail("Fashion Shop", user.getThongTinThanhVien().getMaTV(), "Đổi Mật Khẩu", "Mật khẩu mới của bạn là:" + user.getMatKhau());
         return user;
      }
      catch (Exception e) {
         return user = null;
      }
   }

   @Override
   public PhanQuyenDto forgetPass(PhanQuyenDto user) {
      PhanQuyen phanQuyen = phanQuyenDao.findById(user.getThongTinThanhVien().getMaTV());
      String pass = CryptographyDataUtils.decryptWithAES(phanQuyen.getMatKhau());
      AutoMailTask autoMail = new AutoMailTask();
      autoMail.sendMail("Fashion Shop", user.getThongTinThanhVien().getMaTV(), "Quên Mật Khẩu", "Mật khẩu của bạn là:" + pass);
      return user;
   }

   @Override
   public List<HoaDonDto> getAllBill(ThongTinThanhVienDto user) {
      List<HoaDonDto> hoaDonDtos = new ArrayList<HoaDonDto>();
      hoaDonDtos = mapper.mapCollection(hoaDonDao.getAllBillByUser(user.getMaTV()), HoaDonDto.class);
      return hoaDonDtos;
   }

   @Override
   public HoaDonFullDto getDetailBill(String idHD, String eMail) {
      HoaDonFullDto hoaDonDto = new HoaDonFullDto();
      HoaDonFull hoaDonFull = hoaDonFullDao.findById(idHD);
      try {
         hoaDonDto = mapper.map(hoaDonFull, HoaDonFullDto.class, HOA_DON_FULL_MAP_ID);
      }
      catch (Exception ex) {
         System.out.print(ex);
      }
      return  hoaDonDto;
   }

   @Override
   public HoaDonFullDto getDetailBillPopup(String idHD, String eMail) {
      HoaDonFullDto hoaDonDto = new HoaDonFullDto();
      HoaDonFull hoaDonFull = hoaDonFullDao.findById(idHD);
      if(hoaDonFull != null && eMail.equals(hoaDonFull.getThongTinThanhVien().getMaTV())) {
         try {
            hoaDonDto = mapper.map(hoaDonFull, HoaDonFullDto.class, HOA_DON_FULL_MAP_ID);
         }
         catch (Exception ex) {
            System.out.print(ex);
         }
      }
      else {
         return null;
      }

      return  hoaDonDto;
   }
}
